var http = require('http');
var httpProxy = require('http-proxy');
var fs=require('fs');
//var _global = require('./config.js');

var proxy = httpProxy.createProxyServer({});

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    proxyReq.setHeader('Host', 'lluvia.vip');
});
proxy.on('errror',function(err){
	console.error('我崩了，',err);
})

var server = http.createServer(function(req, res) {
	var resurl=process.cwd()+req.url;	
	console.log(req.url);
	if(req.url.indexOf('.')>0){	
		if(fs.existsSync(resurl)){
			fs.createReadStream(resurl).pipe(res);
		}else{
			res.write(resurl+' 文件不存在');
			res.end();
		}
	}else{
    	proxy.web(req, res, {   target: 'http://lluvia.vip/'    });		
	}
});

console.log("listening on port 8000");
server.listen(8000);